/**
 * express 结合 https
 */

import express from 'express'

import https from 'node:https'

import { readFileSync } from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

const app = express()

// 当前目录，假设   在 http目录的外层启动程序
const dir = fileURLToPath(new URL('./', import.meta.url))

/**
 * @type {https.RequestOptions}
 */
const options = {
  // 私钥
  key: readFileSync(path.resolve(dir, 'server.key')),
  // 证书文件
  cert: readFileSync(path.resolve(dir, 'server.crt'))
}

const server = https.createServer(options, app)

app.get('/', (req, res) => {
  const indexFile = readFileSync(path.resolve(dir, '03.html'))
  res.end(indexFile)
})


app.get('/api/getInfo', (req, res) => {
  res.json({
    code: 1,
    data: {
      name: '张飒',
      age: 10
    }
  })
})

server.listen(8081, () => {
  console.log('express + https');
  console.log('https://localhost:8081');
})
